package com.itheima.controller;


import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
//发送验证码
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    //发送预约验证码
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        //随机生成一个4位数的验证码
        //String validateCode = ValidateCodeUtils.generateValidateCode4String(4);
        String validateCode="4567";
//        try {
//            //发送信息
//            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode);
//        } catch (ClientException e) {
//            e.printStackTrace();
//            //发送信息失败
//            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
//        }
        //将验证码保存到Redis中
        Jedis resource = jedisPool.getResource();
        resource.setex(telephone + RedisMessageConstant.SENDTYPE_ORDER,300,validateCode);
        //将资源手动进行归还
        resource.close();
        //发送成功需要返回一个数据
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }


    //快速登录验证码
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        //随机生成一个4位数的验证码
        //String validateCode = ValidateCodeUtils.generateValidateCode4String(4);
        String validateCode="123456";
//        try {
//            //发送信息
//            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode);
//        } catch (ClientException e) {
//            e.printStackTrace();
//            //发送信息失败
//            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
//        }
        //将验证码保存到Redis中
        Jedis resource = jedisPool.getResource();
        resource.setex(telephone + RedisMessageConstant.SENDTYPE_LOGIN,300,validateCode);
        //将资源手动进行归还
        resource.close();
        //发送成功需要返回一个数据
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

}
